// 增强版僵尸系统 - 已整合到zombie.js，此文件已禁用
(function() {
    'use strict';
    
    console.log('🧟 增强版僵尸系统已禁用 - 功能已整合到主zombie.js文件');
    return; // 直接退出，避免冲突
    
    // 扩展原有Zombie类，添加新类型
    if (typeof Zombie !== 'undefined') {
        // 保存原始的Zombie类方法
        const originalGetZombieStats = Zombie.prototype.getZombieStats;
        const originalUpdate = Zombie.prototype.update;
        const originalRender = Zombie.prototype.render;
        const originalTakeDamage = Zombie.prototype.takeDamage;
        const originalDie = Zombie.prototype.die;
        
        // 扩展getZombieStats方法
        Zombie.prototype.getZombieStats = function(type, level = 1) {
            // 如果是原有类型，使用原方法
            const originalTypes = ['normal', 'fast', 'tank', 'spitter'];
            if (originalTypes.includes(type)) {
                return originalGetZombieStats.call(this, type, level);
            }
            
            // 新僵尸类型的属性
            const newZombieStats = {
                // 爆炸型僵尸 - 死亡时爆炸伤害周围单位
                'exploder': {
                    name: '爆炸僵尸',
                    health: 80,
                    speed: 80,
                    damage: 30,
                    color: '#FF4500',
                    size: 25,
                    explosionRadius: 100,
                    explosionDamage: 60,
                    special: 'explosion_on_death'
                },
                
                // 电击型僵尸 - 放电攻击
                'electric': {
                    name: '电击僵尸',
                    health: 100,
                    speed: 70,
                    damage: 35,
                    color: '#00FFFF',
                    size: 22,
                    electricRange: 120,
                    electricDamage: 25,
                    electricCooldown: 3000,
                    special: 'electric_attack'
                },
                
                // 隐身型僵尸 - 间歇性隐身
                'stealth': {
                    name: '隐身僵尸',
                    health: 70,
                    speed: 120,
                    damage: 40,
                    color: '#8A2BE2',
                    size: 20,
                    stealthDuration: 2000,
                    stealthCooldown: 5000,
                    special: 'stealth_ability'
                },
                
                // 巨型僵尸 - 超大型，极高血量
                'giant': {
                    name: '巨型僵尸',
                    health: 300,
                    speed: 40,
                    damage: 80,
                    color: '#8B4513',
                    size: 50,
                    special: 'giant_size'
                },
                
                // 分裂型僵尸 - 死亡时分裂成小僵尸
                'splitter': {
                    name: '分裂僵尸',
                    health: 120,
                    speed: 60,
                    damage: 25,
                    color: '#FF1493',
                    size: 30,
                    splitCount: 3,
                    splitType: 'normal',
                    special: 'split_on_death'
                },
                
                // 治愈型僵尸 - 能治愈其他僵尸
                'healer': {
                    name: '治愈僵尸',
                    health: 90,
                    speed: 50,
                    damage: 20,
                    color: '#32CD32',
                    size: 24,
                    healRange: 80,
                    healAmount: 20,
                    healCooldown: 4000,
                    special: 'heal_others'
                },
                
                // 狂暴型僵尸 - 血量越低速度越快
                'berserker': {
                    name: '狂暴僵尸',
                    health: 150,
                    speed: 60,
                    damage: 50,
                    color: '#DC143C',
                    size: 28,
                    maxSpeedMultiplier: 2.5,
                    special: 'berserker_rage'
                }
            };
            
            const stats = newZombieStats[type];
            if (!stats) {
                console.warn(`未知的僵尸类型: ${type}`);
                return originalGetZombieStats.call(this, 'normal', level);
            }
            
            // 根据等级调整属性
            const levelMultiplier = 1 + (level - 1) * 0.3;
            return {
                ...stats,
                health: Math.floor(stats.health * levelMultiplier),
                damage: Math.floor(stats.damage * levelMultiplier),
                speed: Math.floor(stats.speed * Math.min(levelMultiplier, 1.5)) // 速度增长有上限
            };
        };
        
        // 保存原始构造函数
        const originalConstructor = Zombie;
        
        // 扩展构造函数来保存完整的stats
        const enhancedZombieInit = function() {
            // 确保新僵尸类型有完整的stats
            if (this.type && !['normal', 'fast', 'tank', 'spitter'].includes(this.type)) {
                this.stats = this.getZombieStats(this.type, this.level);
                
                // 如果有size属性，更新半径
                if (this.stats.size) {
                    this.radius = this.stats.size / 2;
                }
            }
        };
        
        // 扩展update方法
        Zombie.prototype.update = function(deltaTime, player) {
            // 确保增强版僵尸有stats
            if (!this.stats && this.type && !['normal', 'fast', 'tank', 'spitter'].includes(this.type)) {
                enhancedZombieInit.call(this);
            }
            
            // 调用原始update
            originalUpdate.call(this, deltaTime, player);
            
            // 处理新僵尸类型的特殊能力
            this.updateSpecialAbilities(deltaTime, player);
        };
        
        // 新增特殊能力更新方法
        Zombie.prototype.updateSpecialAbilities = function(deltaTime, player) {
            if (!this.stats || !this.stats.special) return;
            
            const currentTime = Date.now();
            
            switch (this.stats.special) {
                case 'electric_attack':
                    this.updateElectricAttack(currentTime, player);
                    break;
                    
                case 'stealth_ability':
                    this.updateStealthAbility(currentTime);
                    break;
                    
                case 'berserker_rage':
                    this.updateBerserkerRage();
                    break;
                    
                case 'heal_others':
                    this.updateHealAbility(currentTime);
                    break;
            }
        };
        
        // 电击攻击
        Zombie.prototype.updateElectricAttack = function(currentTime, player) {
            if (!player || !this.stats || !this.stats.electricCooldown) return;
            
            if (!this.lastElectricAttack) this.lastElectricAttack = 0;
            
            if (currentTime - this.lastElectricAttack > this.stats.electricCooldown) {
                const distToPlayer = Math.sqrt(
                    Math.pow(this.x - player.x, 2) + Math.pow(this.y - player.y, 2)
                );
                
                if (distToPlayer <= (this.stats.electricRange || 120)) {
                    this.performElectricAttack(player);
                    this.lastElectricAttack = currentTime;
                }
            }
        };
        
        Zombie.prototype.performElectricAttack = function(player) {
            console.log('⚡ 电击僵尸发动电击攻击！');
            
            // 对玩家造成伤害
            if (player && typeof player.takeDamage === 'function' && this.stats && this.stats.electricDamage) {
                player.takeDamage(this.stats.electricDamage);
            }
            
            // 创建电击特效
            this.createElectricEffect();
        };
        
        Zombie.prototype.createElectricEffect = function() {
            try {
                if (window.gameEngine && window.gameEngine.particles) {
                    // 创建电击粒子效果
                    for (let i = 0; i < 15; i++) {
                        const angle = (Math.PI * 2 / 15) * i;
                        const distance = this.stats.electricRange;
                        
                        window.gameEngine.particles.push({
                            x: this.x + Math.cos(angle) * distance * Math.random(),
                            y: this.y + Math.sin(angle) * distance * Math.random(),
                            vx: (Math.random() - 0.5) * 100,
                            vy: (Math.random() - 0.5) * 100,
                            life: 0.5,
                            maxLife: 0.5,
                            color: '#00FFFF',
                            size: Math.random() * 4 + 2
                        });
                    }
                }
            } catch (error) {
                console.error('创建电击特效失败:', error);
            }
        };
        
        // 隐身能力
        Zombie.prototype.updateStealthAbility = function(currentTime) {
            if (!this.stats || !this.stats.stealthDuration || !this.stats.stealthCooldown) return;
            
            if (!this.stealthTimer) {
                this.stealthTimer = currentTime;
                this.isStealthed = false;
                this.alpha = 1;
            }
            
            const timeSinceLastToggle = currentTime - this.stealthTimer;
            const totalCycle = this.stats.stealthDuration + this.stats.stealthCooldown;
            
            if (timeSinceLastToggle >= totalCycle) {
                this.stealthTimer = currentTime;
            }
            
            // 更新隐身状态
            if (timeSinceLastToggle < this.stats.stealthDuration) {
                this.isStealthed = true;
                this.alpha = 0.3; // 半透明
            } else {
                this.isStealthed = false;
                this.alpha = 1;
            }
        };
        
        // 狂暴状态
        Zombie.prototype.updateBerserkerRage = function() {
            if (!this.stats || !this.stats.maxSpeedMultiplier || !this.health || !this.maxHealth) return;
            
            const healthPercent = this.health / this.maxHealth;
            const rageMultiplier = 1 + (1 - healthPercent) * (this.stats.maxSpeedMultiplier - 1);
            
            // 更新速度（血量越低速度越快）
            this.currentSpeed = (this.stats.speed || this.speed || 80) * rageMultiplier;
            
            // 更新颜色（血量越低颜色越红）
            const redness = Math.floor(255 * (2 - rageMultiplier));
            this.currentColor = `rgb(220, ${redness}, ${redness})`;
        };
        
        // 治愈能力
        Zombie.prototype.updateHealAbility = function(currentTime) {
            if (!this.stats || !this.stats.healCooldown) return;
            
            if (!this.lastHeal) this.lastHeal = 0;
            
            if (currentTime - this.lastHeal > this.stats.healCooldown) {
                this.performHealAbility();
                this.lastHeal = currentTime;
            }
        };
        
        Zombie.prototype.performHealAbility = function() {
            if (!this.stats || !this.stats.healRange || !this.stats.healAmount) return;
            
            try {
                if (window.gameEngine && window.gameEngine.zombies) {
                    let healed = 0;
                    
                    window.gameEngine.zombies.forEach(zombie => {
                        if (zombie !== this && zombie.health < zombie.maxHealth) {
                            const distance = Math.sqrt(
                                Math.pow(this.x - zombie.x, 2) + Math.pow(this.y - zombie.y, 2)
                            );
                            
                            if (distance <= this.stats.healRange) {
                                const healAmount = Math.min(
                                    this.stats.healAmount,
                                    zombie.maxHealth - zombie.health
                                );
                                zombie.health += healAmount;
                                healed++;
                                
                                // 治愈特效
                                this.createHealEffect(zombie);
                            }
                        }
                    });
                    
                    if (healed > 0) {
                        console.log(`💚 治愈僵尸治愈了${healed}只僵尸`);
                    }
                }
            } catch (error) {
                console.error('治愈能力执行失败:', error);
            }
        };
        
        Zombie.prototype.createHealEffect = function(target) {
            try {
                if (window.gameEngine && window.gameEngine.particles) {
                    for (let i = 0; i < 8; i++) {
                        window.gameEngine.particles.push({
                            x: target.x + (Math.random() - 0.5) * 40,
                            y: target.y + (Math.random() - 0.5) * 40,
                            vx: 0,
                            vy: -Math.random() * 50 - 20,
                            life: 1,
                            maxLife: 1,
                            color: '#32CD32',
                            size: Math.random() * 6 + 3
                        });
                    }
                }
            } catch (error) {
                console.error('创建治愈特效失败:', error);
            }
        };
        
        // 扩展takeDamage方法
        Zombie.prototype.takeDamage = function(damage) {
            // 隐身状态减少受到的伤害
            if (this.isStealthed) {
                damage = Math.floor(damage * 0.7); // 隐身时减少30%伤害
            }
            
            // 调用原始takeDamage
            originalTakeDamage.call(this, damage);
        };
        
        // 扩展die方法
        Zombie.prototype.die = function() {
            // 处理死亡时的特殊能力
            if (this.stats && this.stats.special) {
                switch (this.stats.special) {
                    case 'explosion_on_death':
                        this.explodeOnDeath();
                        break;
                        
                    case 'split_on_death':
                        this.splitOnDeath();
                        break;
                }
            }
            
            // 调用原始die方法
            originalDie.call(this);
        };
        
        // 死亡爆炸
        Zombie.prototype.explodeOnDeath = function() {
            if (!this.stats || !this.stats.explosionRadius || !this.stats.explosionDamage) return;
            
            console.log('💥 爆炸僵尸死亡爆炸！');
            
            try {
                // 对周围单位造成伤害
                if (window.player) {
                    const distToPlayer = Math.sqrt(
                        Math.pow(this.x - window.player.x, 2) + Math.pow(this.y - window.player.y, 2)
                    );
                    
                    if (distToPlayer <= this.stats.explosionRadius) {
                        if (typeof window.player.takeDamage === 'function') {
                            window.player.takeDamage(this.stats.explosionDamage);
                            console.log('💥 玩家受到爆炸伤害！');
                        }
                    }
                }
                
                // 对其他僵尸造成伤害
                if (window.gameEngine && window.gameEngine.zombies) {
                    window.gameEngine.zombies.forEach(zombie => {
                        if (zombie !== this) {
                            const distance = Math.sqrt(
                                Math.pow(this.x - zombie.x, 2) + Math.pow(this.y - zombie.y, 2)
                            );
                            
                            if (distance <= this.stats.explosionRadius) {
                                if (typeof zombie.takeDamage === 'function') {
                                    zombie.takeDamage(this.stats.explosionDamage * 0.5); // 对僵尸伤害减半
                                }
                            }
                        }
                    });
                }
                
                // 创建爆炸特效
                this.createExplosionEffect();
                
            } catch (error) {
                console.error('爆炸效果执行失败:', error);
            }
        };
        
        Zombie.prototype.createExplosionEffect = function() {
            try {
                if (window.gameEngine && window.gameEngine.particles) {
                    // 爆炸粒子
                    for (let i = 0; i < 25; i++) {
                        const angle = (Math.PI * 2 * i) / 25;
                        const speed = Math.random() * 200 + 100;
                        
                        window.gameEngine.particles.push({
                            x: this.x,
                            y: this.y,
                            vx: Math.cos(angle) * speed,
                            vy: Math.sin(angle) * speed,
                            life: Math.random() * 2 + 1,
                            maxLife: Math.random() * 2 + 1,
                            color: ['#FF4500', '#FF6347', '#FFD700', '#DC143C'][Math.floor(Math.random() * 4)],
                            size: Math.random() * 8 + 4
                        });
                    }
                }
            } catch (error) {
                console.error('创建爆炸粒子失败:', error);
            }
        };
        
        // 死亡分裂
        Zombie.prototype.splitOnDeath = function() {
            if (!this.stats || !this.stats.splitCount || !this.stats.splitType) return;
            
            console.log('🔄 分裂僵尸死亡分裂！');
            
            try {
                if (window.gameEngine && window.gameEngine.zombies && typeof Zombie !== 'undefined') {
                    for (let i = 0; i < this.stats.splitCount; i++) {
                        const angle = (Math.PI * 2 / this.stats.splitCount) * i;
                        const distance = 40;
                        
                        const splitX = this.x + Math.cos(angle) * distance;
                        const splitY = this.y + Math.sin(angle) * distance;
                        
                        // 创建小僵尸（等级降低）
                        const splitZombie = new Zombie(splitX, splitY, this.stats.splitType, Math.max(1, this.level - 1));
                        splitZombie.health = Math.floor(splitZombie.health * 0.6); // 分裂后血量减少
                        splitZombie.maxHealth = splitZombie.health;
                        if (splitZombie.size) {
                            splitZombie.size = Math.floor(splitZombie.size * 0.8); // 体型稍小
                        }
                        
                        window.gameEngine.zombies.push(splitZombie);
                    }
                    
                    console.log(`🔄 分裂产生${this.stats.splitCount}只小僵尸`);
                }
            } catch (error) {
                console.error('分裂效果执行失败:', error);
            }
        };
        
        // 扩展render方法
        Zombie.prototype.render = function(ctx) {
            if (this.alpha !== undefined) {
                ctx.save();
                ctx.globalAlpha = this.alpha;
            }
            
            // 调用原始render
            originalRender.call(this, ctx);
            
            if (this.alpha !== undefined) {
                ctx.restore();
            }
            
            // 渲染特殊效果
            this.renderSpecialEffects(ctx);
        };
        
        // 渲染特殊效果
        Zombie.prototype.renderSpecialEffects = function(ctx) {
            if (!this.stats || !this.stats.special) return;
            
            ctx.save();
            
            switch (this.stats.special) {
                case 'electric_attack':
                    this.renderElectricAura(ctx);
                    break;
                    
                case 'heal_others':
                    this.renderHealAura(ctx);
                    break;
                    
                case 'explosion_on_death':
                    this.renderExplosionWarning(ctx);
                    break;
            }
            
            ctx.restore();
        };
        
        Zombie.prototype.renderElectricAura = function(ctx) {
            if (this.lastElectricAttack && Date.now() - this.lastElectricAttack < 1000) {
                ctx.strokeStyle = '#00FFFF';
                ctx.lineWidth = 2;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.stats.electricRange, 0, Math.PI * 2);
                ctx.stroke();
            }
        };
        
        Zombie.prototype.renderHealAura = function(ctx) {
            const time = Date.now() * 0.005;
            const alpha = 0.3 + Math.sin(time) * 0.2;
            
            ctx.globalAlpha = alpha;
            ctx.strokeStyle = '#32CD32';
            ctx.lineWidth = 2;
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.stats.healRange, 0, Math.PI * 2);
            ctx.stroke();
        };
        
        Zombie.prototype.renderExplosionWarning = function(ctx) {
            const healthPercent = this.health / this.maxHealth;
            if (healthPercent < 0.3) { // 血量低于30%时显示警告
                const time = Date.now() * 0.01;
                const alpha = 0.5 + Math.sin(time) * 0.3;
                
                ctx.globalAlpha = alpha;
                ctx.strokeStyle = '#FF4500';
                ctx.lineWidth = 3;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.stats.explosionRadius, 0, Math.PI * 2);
                ctx.stroke();
                
                // 警告文字
                ctx.fillStyle = '#FF0000';
                ctx.font = '14px monospace';
                ctx.textAlign = 'center';
                ctx.fillText('⚠', this.x, this.y - this.size - 20);
            }
        };
        
        console.log('✅ 增强版僵尸系统已扩展Zombie类');
    } else {
        console.error('❌ Zombie类未找到，无法扩展');
    }
    
})();